package com.bra.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * @Title: SPHelper.java
 * @Package com.gxyk.wifi.utils
 * @Description: TODO(SharedPreferences保存读取工具类)
 *
 * @date 2015年9月1日 下午4:21:34
 */
public class SPHelper {
	public static String SP_NAME = "config";

	/**
	 * SP保存字符串
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void saveString(Context context, String key, String value) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME,
				Context.MODE_PRIVATE);
		if (sp != null) {
			Editor mEditor = sp.edit();
			mEditor.putString(key, value);
			mEditor.commit();
		}
	}

	/**
	 * 读取字符串
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static String readString(Context context, String key) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME,
				Context.MODE_PRIVATE);
		if (sp != null) {
			return sp.getString(key, "");
		}
		return "";
	}

	/**
	 * SP保存Int
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void saveInt(Context context, String key, int value) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME,
				Context.MODE_PRIVATE);
		if (sp != null) {
			Editor mEditor = sp.edit();
			mEditor.putInt(key, value);
			mEditor.commit();
		}
	}

	/**
	 * 读取Int
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static int readInt(Context context, String key, int defValue) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME,
				Context.MODE_PRIVATE);
		if (sp != null) {
			return sp.getInt(key, defValue);
		}
		return defValue;
	}

	/**
	 * 保存Boolean变量
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void saveBoolean(Context context, String key, boolean value) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME,
				Context.MODE_PRIVATE);
		if (sp != null) {
			Editor mEditor = sp.edit();
			mEditor.putBoolean(key, value);
			mEditor.commit();
		}
	}

	/**
	 * 读取Boolean
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static boolean readBoolean(Context context, String key,
			boolean defValue) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME,
				Context.MODE_PRIVATE);
		if (sp != null) {
			return sp.getBoolean(key, defValue);
		}
		return defValue;
	}
}
